//
//  Product_Details_ViewController.m
//  Pacific_Computer
//
//  Created by dlios on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Product_Details_ViewController.h"
#import "UIColor+AddColor.h"
#import "NetHandler.h"
#import "Information.h"
#import "Product_Information_ViewController.h"
#import "AppDelegate.h"
#import "WeiboSDK.h"
#import "WBHttpRequest.h"

@interface Product_Details_ViewController () <UIScrollViewDelegate,UIWebViewDelegate,UITableViewDataSource,UITableViewDelegate,WBHttpRequestDelegate>

@property (nonatomic,retain) NSMutableArray *nameArr;
@property (nonatomic,retain) NSMutableArray *viewArr;
@property (nonatomic,retain) UIScrollView *scroll;
@property (nonatomic,retain) NSMutableArray *urlArr;
@property (nonatomic,assign) NSInteger urlNum;
@property (nonatomic,retain) UITableView *tabView;

@property (nonatomic,retain) UIWebView *web;

@property (nonatomic,retain) UIView *lodingView;
@property (nonatomic,retain) UIActivityIndicatorView *actView;
@property (nonatomic,retain) NSMutableArray *arr;
@property (nonatomic, retain) NSString *str;

@property(nonatomic, retain) UIView *view1;

@end

@implementation Product_Details_ViewController

- (void)dealloc
{
    [_nameArr release];
    [_viewArr release];
    [_scroll release];
    [_urlArr release];
    [_web release];
    [_lodingView release];
    [_actView release];
    [_tabView release];
    [_arr release];
    [_para release];
    [_str release];
    [_search release];
    [_view1 release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.nameArr = [NSMutableArray arrayWithObjects:@"概述", @"参数", @"报价", @"点评", @"资讯", nil];
        self.viewArr = [NSMutableArray array];
        
        self.urlArr = [NSMutableArray array];
        
    }
    return self;
}


- (void)handlerData
{
    if (self.para != nil) {
        
        self.str = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/information/%@?ver=33&pageNo=1&pageSize=20",self.para.parameterId];
    }
    else{
        self.str = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/information/%@?ver=33&pageNo=1&pageSize=20",self.search.productId];
    }
    
    [NetHandler getDataWithUrl:self.str completion:^(NSData *data) {
        
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        NSArray *array = [result objectForKey:@"articleList"];
        self.arr = [NSMutableArray array];
        
        for (NSDictionary *dic in array) {
            Information *inform = [[Information alloc] initWithDictionary:dic];
            [self.arr addObject:inform];
            [inform release];
        }
        
        [self.tabView reloadData];
    }];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"产品详情";
    [self handlerData];
    
//    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 9.59 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 6.74 / 100)];
//    scroll.contentOffset = CGPointMake(0, self.view.bounds.size.height * 9.59 / 100);
//    scroll.scrollEnabled = NO;
//    scroll.backgroundColor = [UIColor huiseColor];
//    [self.view addSubview:scroll];
//    [scroll release];
    if (self.para != nil) {
        
        self.view1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 9.59 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 6.74 / 100)];
        self.view1.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.view1];
        [_view1 release];
    }else{
         self.view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 6.74 / 100)];
        self.view1.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.view1];
        [_view1 release];
    
    }
    
    
    for (NSInteger i = 0; i < self.nameArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(i * self.view.bounds.size.width / 5, 1, self.view.bounds.size.width / 5, self.view.bounds.size.height * 6.74 / 100);

        [button setTitle:[self.nameArr objectAtIndex:i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view1 addSubview:button];
        button.tag = i + 1;
        
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 5.9 / 100, button.bounds.size.width, self.view.bounds.size.height * 0.599 / 100)];
        [button addSubview:view];
        [view release];
        
        if (button.tag == 1) {
            button.tintColor = [UIColor hongse];
            view.backgroundColor = [UIColor hongse];
        }else{
            button.tintColor = [UIColor blackColor];
            view.backgroundColor = [UIColor whiteColor];
        }
        
        [self.viewArr addObject:button];
    }
    
    //=================================================
    if (self.para != nil) {
        self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 16.04 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100)];
    }else{
        self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 7 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100)];
    }
    self.scroll.contentSize = CGSizeMake(self.viewArr.count *self.view.bounds.size.width, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
    self.scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scroll];
    [_scroll release];
    
    
    
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100)];
    self.web.delegate = self;
    [self.scroll addSubview:self.web];
    [self openUrl];
    [_web release];
    
    self.lodingView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 16.04 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100)];
    self.lodingView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.lodingView];
    self.lodingView.alpha = 0.5;
    [_lodingView release];
    
    self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 4, 0, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100) style:UITableViewStylePlain];
    self.tabView.rowHeight = self.view.bounds.size.height * 6.74 / 100;
    self.tabView.backgroundColor = [UIColor huiseColor];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    
    [self.scroll addSubview:self.tabView];
    [_tabView retain];
    

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, self.view.bounds.size.width * 8 / 100, self.view.bounds.size.height * 4.49 / 100);
    [button setImage:[UIImage imageNamed:@"fengxiang2"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(fengxiangAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
}


- (WBMessageObject *)messageToShare
{
    
    
    WBMessageObject *message = [WBMessageObject message];
    if (self.para != nil) {
        WBImageObject *image = [WBImageObject object];
        NSString *path = self.para.idxPic;
        NSURL *url = [NSURL URLWithString:path];
        image.imageData = [NSData dataWithContentsOfURL:url];
        message.imageObject = image;
        
    }else{
        WBImageObject *image = [WBImageObject object];
        NSString *path = self.search.pic;
        NSURL *url = [NSURL URLWithString:path];
        image.imageData = [NSData dataWithContentsOfURL:url];
        message.imageObject = image;
    }
    NSString *str = [NSString stringWithFormat:@"%@ #%@#:",self.para.url,self.para.shortName];
    message.text = str;

    
    
    
    
    return message;
}

- (void)fengxiangAction:(UIButton *)button
{
    AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = @"https://api.weibo.com/oauth2/default.html";
    authRequest.scope = @"all";
    
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:myDelegate.wbtoken];
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
    [WeiboSDK sendRequest:request];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"detarse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    Information *inform = [self.arr objectAtIndex:indexPath.row];
    cell.textLabel.text = inform.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Product_Information_ViewController *information = [[Product_Information_ViewController alloc] init];
    information.inform = [self.arr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:information animated:YES];
    [information release];
    
}


- (void)openUrl
{
    //网址
    if (self.para != nil) {
        NSString *urlOne = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/summary/%@?ver=33",self.para.parameterId];
        [self.urlArr addObject:urlOne];
        
        NSString *urlTwo = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/detail/%@?ver=33",self.para.parameterId];
        [self.urlArr addObject:urlTwo];
        
        NSString *urlThree = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/comparePrice/%@",self.para.parameterId];
        [self.urlArr addObject:urlThree];
        
        NSString *urlFour = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/comments/%@?ver=33",self.para.parameterId];
        [self.urlArr addObject:urlFour];
    }
    else{
        NSString *urlOne = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/summary/%@?ver=33",self.search.productId];
        [self.urlArr addObject:urlOne];
        
        NSString *urlTwo = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/detail/%@?ver=33",self.search.productId];
        [self.urlArr addObject:urlTwo];
        
        NSString *urlThree = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/comparePrice/%@",self.search.productId];
        [self.urlArr addObject:urlThree];
        
        NSString *urlFour = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/comments/%@?ver=33",self.search.productId];
        [self.urlArr addObject:urlFour];
    
    
    
    }
    
    // 请求URL
    NSString *str = [self.urlArr objectAtIndex:self.urlNum];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    
    // 加载请求
    [self.web loadRequest:request];
    
    
}


//Web开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    self.actView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhite)];
    self.actView.frame = CGRectMake(self.view.bounds.size.width * 46.66 / 100, self.view.bounds.size.height * 37.48 / 100, self.view.bounds.size.width * 6.66 / 100, self.view.bounds.size.height * 3.74 / 100);
    //self.actView.center = self.lodingView.center;
    [self.lodingView addSubview:self.actView];
    
    //开始转动
    [self.actView startAnimating];
    [_actView release];
}

//Wen加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.actView stopAnimating];
    [self.lodingView removeFromSuperview];
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.actView stopAnimating];
    [self.lodingView removeFromSuperview];
    
    
}


//self.scroll减速停止方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = self.scroll.contentOffset.x / self.scroll.frame.size.width + 1;
    for (UIButton *buttonStr in self.viewArr) {
        UIView *view = [buttonStr.subviews firstObject];
        if (buttonStr.tag != i) {
            buttonStr.tintColor = [UIColor blackColor];
            view.backgroundColor = [UIColor whiteColor];
        }else{
            buttonStr.tintColor = [UIColor hongse];
            view.backgroundColor = [UIColor hongse];
        }
    }
    
    for (UIView *view in [self.web subviews]) {
        [view removeFromSuperview];
    }
    
    
    self.urlNum = i - 1;
    if (self.urlNum < 4) {
        self.web = [[UIWebView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * self.urlNum, 0, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100)];
        self.web.delegate = self;
        [self.scroll addSubview:self.web];
        
        self.lodingView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 16.04 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100)];
        self.lodingView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:self.lodingView];
        self.lodingView.alpha = 0.5;
        [_lodingView release];
        [self openUrl];
        [_web release];
    }
}


- (void)buttonClicked:(UIButton *)button
{
    self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width * (button.tag - 1), 0);
    
    for (UIButton *buttonStr in self.viewArr) {
        UIView *view = [buttonStr.subviews firstObject];
        if (buttonStr.tag != button.tag) {
            buttonStr.tintColor = [UIColor blackColor];
            view.backgroundColor = [UIColor whiteColor];
        }else{
            buttonStr.tintColor = [UIColor hongse];
            view.backgroundColor = [UIColor hongse];
        }
    }
    
    
    for (UIView *view in [self.web subviews]) {
        [view removeFromSuperview];
    }
    
    
    self.urlNum = button.tag - 1;
    if (self.urlNum < 4) {
        self.web = [[UIWebView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * self.urlNum, 0, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100)];
        self.web.delegate = self;
        [self.scroll addSubview:self.web];
        
        self.lodingView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 16.04 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 76.61 / 100)];
        self.lodingView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:self.lodingView];
        self.lodingView.alpha = 0.5;
        [_lodingView release];
        
        [self openUrl];
        [_web release];
    }

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
